<template>
  <div class="tools-div">
    <el-button type="success" size="small" @click="onAddShowDialog">
      添加
    </el-button>
  </div>

  <!--弹窗表单-->
  <el-dialog v-model="dialogImportVisible" :title="title" width="30%">
    <el-form label-width="120px">
      <el-form-item label="品牌名">
        <el-input v-model="brand.name" />
      </el-form-item>
      <!-- 文件上传 -->
      <el-form-item label="logo">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8200/api/minio/fileUpload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :headers="headers"
        >
          <img v-if="brand.logo" :src="brand.logo" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="dialogImportVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <!---数据展示表格-->
  <el-table :data="list" style="width: 100%" stripe="true">
    <el-table-column prop="name" label="品牌名称" />
    <el-table-column prop="logo" label="Logo" #default="scope">
      <img :src="scope.row.logo" width="50" />
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" />
    <el-table-column label="操作" #default="scope" align="center" width="280">
      <el-button type="primary" size="small" @click="updateShow(scope.row)">
        修改
      </el-button>
      <el-button type="danger" size="small" @click="remove(scope.row)">
        删除
      </el-button>
    </el-table-column>
  </el-table>

  <!-- 分页页码 -->
  <el-pagination
    v-model:current-page="pageParams.page"
    v-model:page-sizes="pageParams.limit"
    :page-sizes="[10, 20, 50, 100]"
    layout="total, sizes, prev, pager, next"
    :total="total"
  />
</template>

<script setup>
import {
  deleteAPI,
  getSelectAllAPI,
  postSaveAPI,
  putUpdateAPI,
} from '@/api/brand'
import { useApp } from '@/pinia/modules/app'
import { ElMessage, ElMessageBox } from 'element-plus'
import { onMounted, ref } from 'vue'
//===========================查询所有的品牌数据============================================
//表格数据展示
const list = ref([])
//分页总数据条数
const total = ref(0)
//分页当前页和每页记录数数据
const pageParamsForm = {
  page: 1, // 页码
  limit: 10, // 每页记录数
}
const pageParams = ref(pageParamsForm)
//弹窗表单标题
const title = ref('添加表单')
//控制表单显示
const dialogImportVisible = ref(false)
//调用后端接口
const getSelectAllData = async () => {
  const { data } = await getSelectAllAPI(
    pageParams.value.page,
    pageParams.value.limit
  )
  list.value = data.list
  total.value = data.total
}

onMounted(() => {
  getSelectAllData()
})
//========================添加和修改=======================================
//添加表单数据模型
const brand = ref({
  id: '',
  name: '',
  logo: '',
})
//打开添加表单
const onAddShowDialog = () => {
  dialogImportVisible.value = true
  title.value = '添加表单'
  brand.value = {}
}
//提交按钮事件
const submit = async () => {
  if (!brand.value.id) {
    const { code, data, message } = await postSaveAPI(brand.value)
    if (code === 200) {
      ElMessage.success('添加成功!')
      dialogImportVisible.value = false
      getSelectAllData()
    }
  } else {
    const { code, data, message } = await putUpdateAPI(brand.value)
    if (code === 200) {
      ElMessage.success('修改成功!')
      dialogImportVisible.value = false
      getSelectAllData()
    }
  }
}
///////////////////////////////////////////////////////////////////////////////文件上传
// 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
const headers = {
  token: useApp().authorization.token,
}

// 图像上传成功以后的事件处理函数
const handleAvatarSuccess = (response, uploadFile) => {
  brand.value.logo = response.data
}

//===================================修改弹窗打开==================================================
const updateShow = row => {
  title.value = '修改表单'
  dialogImportVisible.value = true
  brand.value = { ...row }
}

//================================删除=========================================================
const remove = row => {
  ElMessageBox.confirm('是否确定删除?', '删除弹窗', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await deleteAPI(row.id)
    if (code === 200) {
      ElMessage.success('删除成功!')
      pageParams.value.page = 1
      getSelectAllData()
    }
  })
}
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
